home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / c / c_os3_dt402.lha / c_dt / dt_source / libfuncs.c < prev    next >
C/C++ Source or Header  |  1996-09-02  |  8KB  |  367 lines

  1. /*
  2. **      $VER: libfuncs.h 40.1 (2.9.96)
  3. **
  4. **      datatype functions
  5. **
  6. **      (C) Copyright 1996 Andreas R. Kleinert
  7. **      All Rights Reserved.
  8. */
  9.  
  10. #define __USE_SYSBASE
  11.  
  12. #ifndef EXEC_TYPES_H
  13. #include <exec/types.h>
  14. #endif /* EXEC_TYPES_H */
  15.  
  16. #ifndef EXEC_MEMORY_H
  17. #include <exec/memory.h>
  18. #endif /* EXEC_MEMORY_H */
  19.  
  20. #ifndef GRAPHICS_GFXBASE_H
  21. #include <graphics/gfxbase.h>
  22. #endif /* GRAPHICS_GFXBASE_H */
  23.  
  24. #ifndef GRAPHICS_VIEW_H
  25. #include <graphics/view.h>
  26. #endif /* GRAPHICS_VIEW_H */
  27.  
  28. #include <datatypes/pictureclass.h>
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/intuition.h>
  33. #include <proto/graphics.h>
  34. #include <proto/utility.h>
  35. #include <proto/datatypes.h>
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40.  
  41. #include <class/classbase.h>
  42. #include "libfuncs.h"
  43.  
  44. #ifdef GLOBAL
  45. #undef GLOBAL
  46. #endif /* GLOBAL */
  47.  
  48. #define N (NULL)
  49.  
  50. Class * __saveds __asm ObtainPicClass ( register __a6 struct ClassBase *cb)
  51. {
  52.  return (cb->cb_Class);
  53. }
  54.  
  55. ULONG setdtattrs (struct ClassBase * cb, Object * o, ULONG data,...)
  56. {
  57.  return (SetDTAttrsA (o, NULL, NULL, (struct TagItem *) & data));
  58. }
  59.  
  60. ULONG getdtattrs (struct ClassBase * cb, Object * o, ULONG data,...)
  61. {
  62.  return (GetDTAttrsA (o, (struct TagItem *) & data));
  63. }
  64.  
  65.  
  66. extern char __aligned ExLibName [];
  67.  
  68. Class *initClass (struct ClassBase * cb)
  69. {
  70.  Class *cl;
  71.  
  72.  if (cl = MakeClass (&ExLibName[0], PICTUREDTCLASS, NULL, NULL, 0L))
  73.   {
  74.    cl->cl_Dispatcher.h_Entry = (ULONG) Dispatch;
  75.    cl->cl_UserData = (ULONG) cb;
  76.    AddClass (cl);
  77.   }
  78.  
  79.  return (cl);
  80. }
  81.  
  82. ULONG __saveds __asm Dispatch ( register __a0 Class * cl, register __a2 Object * o, register __a1 Msg msg)
  83. {
  84.  struct ClassBase *cb = (struct ClassBase *) cl->cl_UserData;
  85.  ULONG retval;
  86.  
  87.  switch (msg->MethodID)
  88.   {
  89.    case OM_NEW:
  90.     {
  91.      if (retval = DoSuperMethodA (cl, o, msg))
  92.       {
  93.        if (!GetGfxData (cb, cl, (Object *) retval, ((struct opSet *) msg)->ops_AttrList))
  94.         {
  95.          CoerceMethod (cl, (Object *) retval, OM_DISPOSE);
  96.  
  97.      return(NULL);
  98.         }
  99.       }
  100.      break;
  101.     }
  102.    default:
  103.     {
  104.      retval = (ULONG) DoSuperMethodA (cl, o, msg);
  105.      break;
  106.     }
  107.   }
  108.  
  109.  return (retval);
  110. }
  111.  
  112. ULONG __saveds __asm GetGfxData ( register __a6 struct ClassBase * cb, register __a0 Class * cl, register __a2 Object * o, register __a1 struct TagItem * attrs)
  113. {
  114.  return( SVLI_ReadIntoBitMap(cb, o, attrs) );
  115. }
  116.  
  117. ULONG __saveds __stdargs dt_GetBestModeID(ULONG width, ULONG height, ULONG depth);
  118. void __saveds __stdargs SVLI_SkipComment(BPTR handle, UBYTE *buf, ULONG *cnt);
  119.  
  120. ULONG __saveds __stdargs SVLI_ReadIntoBitMap(struct ClassBase *cb, Object * o, struct TagItem * attrs)
  121. {
  122.  struct BitMapHeader *bmhd;
  123.  
  124.  BOOL success = TRUE;
  125.  
  126.  struct RastPort __aligned trp;
  127.  struct RastPort __aligned rp;
  128.  
  129.  struct BitMap *bm, *tbm;
  130.  
  131.  ULONG i, width, height, depth, maxval;
  132.  
  133.  UBYTE *readname, *buffer;
  134.  
  135.  struct ColorRegister *cmap;
  136.  LONG *cregs;
  137.  
  138.  BPTR fh;
  139.  
  140.  UBYTE wstr[16], hstr[16], dstr[16];
  141.  UWORD ID;
  142.  
  143.  
  144.  readname = (UBYTE *) GetTagData (DTA_Name, NULL, attrs);
  145.  getdtattrs (cb, o, PDTA_BitMapHeader, &bmhd, TAG_DONE, N);
  146.  
  147.  
  148.  fh = Open(readname, MODE_OLDFILE);
  149.  if(!fh) return(FALSE);
  150.  
  151.  
  152.  FRead(fh, &ID, 2, 1);
  153.  FRead(fh, &wstr[0], 1, 1); /* filter CR */
  154.  
  155.  
  156.  /* get Width */
  157.  
  158.  for(i=0; i<16; i++)
  159.   {
  160.    if(!FRead(fh, &wstr[i], 1, 1))
  161.     {
  162.      Close(fh);
  163.      return(FALSE);
  164.     }
  165.  
  166.    if(  wstr[i]=='#') SVLI_SkipComment(fh, wstr, &i);
  167.    if( (wstr[i]==' ') || (wstr[i]==0xa) || (wstr[i]==13) )
  168.     {
  169.      wstr[i] = (UBYTE) '\0';
  170.      break;
  171.     }
  172.   }
  173.  
  174.  /* get Height */
  175.  
  176.  for(i=0; i<16; i++)
  177.   {
  178.    if(!FRead(fh, &hstr[i], 1, 1))
  179.     {
  180.      Close(fh);
  181.      return(FALSE);
  182.     }
  183.  
  184.    if(  hstr[i]=='#') SVLI_SkipComment(fh, hstr, &i);
  185.    if( (hstr[i]==' ') || (hstr[i]==0xa) || (hstr[i]==13) )
  186.     {
  187.      hstr[i] = (UBYTE) '\0';
  188.      break;
  189.     }
  190.   }
  191.  
  192.  /* Maxval */
  193.  
  194.  for(i=0; i<16; i++)
  195.   {
  196.    if(!FRead(fh, &dstr[i], 1, 1))
  197.     {
  198.      Close(fh);
  199.      return(FALSE);
  200.     }
  201.  
  202.    if(  dstr[i]=='#') SVLI_SkipComment(fh, dstr, &i);
  203.    if( (dstr[i]==' ') || (dstr[i]==0xa) || (dstr[i]==13) )
  204.     {
  205.      dstr[i] = (UBYTE) '\0';
  206.      break;
  207.     }
  208.   }
  209.  
  210.  width  = (ULONG) atol(wstr);
  211.  height = (ULONG) atol(hstr);
  212.  maxval = (ULONG) atol(dstr);
  213.  if(maxval > 255)
  214.   {
  215.    Close(fh);
  216.    return(FALSE);
  217.   }
  218.  
  219.  bmhd->bmh_Width  = (bmhd->bmh_PageWidth  = width);
  220.  bmhd->bmh_Height = (bmhd->bmh_PageHeight = height);
  221.  depth            = (bmhd->bmh_Depth = 8);
  222.  
  223.  setdtattrs(cb, o, PDTA_NumColors,      256,
  224.                    TAG_DONE,            N);
  225.  
  226.  getdtattrs(cb, o, PDTA_ColorRegisters, (ULONG) &cmap,
  227.                    PDTA_CRegs,            &cregs,
  228.                    TAG_DONE,            N);
  229.  
  230.  if( (!cmap) || (!cregs) )
  231.   {
  232.    success = FALSE;
  233.   }else
  234.   {
  235.    if(tbm = AllocBitMap (bmhd->bmh_Width, 1, bmhd->bmh_Depth, BMF_CLEAR, NULL))
  236.     {
  237.      InitRastPort (&trp);
  238.      trp.BitMap = tbm;
  239.  
  240.      if (bm = AllocBitMap (bmhd->bmh_Width, bmhd->bmh_Height, bmhd->bmh_Depth, BMF_CLEAR, NULL))
  241.       {
  242.        InitRastPort (&rp);
  243.        rp.BitMap = bm;
  244.  
  245.        buffer = (APTR) AllocVec(width + 1024, MEMF_CLEAR|MEMF_PUBLIC);
  246.        if(buffer)
  247.         {
  248.          for(i=0; i<height; i++)
  249.           {
  250.            FRead(fh, buffer, width, 1);
  251.  
  252.            WritePixelLine8(&rp, 0, i, width, buffer, &trp);
  253.           }
  254.  
  255.          for(i=0; i<maxval; i++)
  256.           {
  257.            cmap->red   = i * (256 / maxval);
  258.            cmap->green = i * (256 / maxval);
  259.            cmap->blue  = i * (256 / maxval);
  260.            cmap++;
  261.  
  262.            cregs[i * 3    ] = ((LONG)i * (256 / maxval))<<24;
  263.            cregs[i * 3 + 1] = ((LONG)i * (256 / maxval))<<24;
  264.            cregs[i * 3 + 2] = ((LONG)i * (256 / maxval))<<24;
  265.           }
  266.  
  267.          cmap->red   = 255;
  268.          cmap->green = 255;
  269.          cmap->blue  = 255;
  270.          cmap++;
  271.  
  272.          cregs[i * 3    ] = ((LONG)255)<<24;
  273.          cregs[i * 3 + 1] = ((LONG)255)<<24;
  274.          cregs[i * 3 + 2] = ((LONG)255)<<24;
  275.  
  276.          setdtattrs (cb, o,
  277.                  DTA_ObjName,    readname,
  278.              DTA_NominalHoriz,    bmhd->bmh_Width,
  279.              DTA_NominalVert,    bmhd->bmh_Height,
  280.              PDTA_BitMap,    bm,
  281.              PDTA_ModeID,    dt_GetBestModeID(bmhd->bmh_Width, bmhd->bmh_Height, 8),
  282.              TAG_DONE);
  283.  
  284.          FreeVec(buffer);
  285.         }else
  286.         {
  287.          success = FALSE;
  288.          SetIoErr (ERROR_NO_FREE_STORE);
  289.         }
  290.       }else
  291.       {
  292.        success = FALSE;
  293.        SetIoErr (ERROR_NO_FREE_STORE);
  294.       }
  295.  
  296.      FreeBitMap(tbm);
  297.     }else
  298.     {
  299.      success = FALSE;
  300.      SetIoErr (ERROR_NO_FREE_STORE);
  301.     }
  302.   }
  303.  
  304.  Close(fh);
  305.  
  306.  return(success);
  307. }
  308.  
  309. ULONG __saveds __stdargs dt_GetBestModeID(ULONG width, ULONG height, ULONG depth)
  310. {
  311.  ULONG mode_id = N;
  312.  
  313.  mode_id = BestModeID(BIDTAG_NominalWidth,  width,
  314.                       BIDTAG_NominalHeight, height,
  315.                       BIDTAG_DesiredWidth,  width,
  316.                       BIDTAG_DesiredHeight, height,
  317.                       BIDTAG_Depth,         depth,
  318.                       TAG_END);
  319.  
  320.  if(!mode_id) /* BestModeID failed */
  321.   {
  322.    /* Uses OverScan values for checking. */
  323.    /* Assumes an ECS-System.             */
  324.  
  325.         if(width > 724 && depth < 3)   mode_id = SUPER_KEY;
  326.    else if(width > 362 && depth < 5)   mode_id = HIRES_KEY;
  327.    else                                mode_id = LORES_KEY;
  328.  
  329.    if(!ModeNotAvailable(mode_id | PAL_MONITOR_ID))    /* for PAL  Systems */
  330.     {
  331.      if(height > 283) mode_id |= LACE;
  332.  
  333.      mode_id |= PAL_MONITOR_ID;
  334.     }else
  335.     {
  336.      if(!ModeNotAvailable(mode_id | NTSC_MONITOR_ID)) /* for NTSC Systems */
  337.       {
  338.        if(height > 241) mode_id |= LACE;
  339.  
  340.        mode_id |= NTSC_MONITOR_ID;
  341.       }
  342.     }
  343.   }
  344.  
  345.  if(!(mode_id & 0xFFFF0000))
  346.   {
  347.          if(!ModeNotAvailable(mode_id |  PAL_MONITOR_ID)) mode_id |=  PAL_MONITOR_ID;
  348.     else if(!ModeNotAvailable(mode_id | NTSC_MONITOR_ID)) mode_id |= NTSC_MONITOR_ID;
  349.   }
  350.  
  351.  if(mode_id == INVALID_ID) mode_id = NULL;
  352.  
  353.  return(mode_id);
  354. }
  355.  
  356. void __saveds __stdargs SVLI_SkipComment(BPTR handle, UBYTE *buf, ULONG *cnt)
  357. {
  358.  do
  359.   {
  360.    FRead(handle, buf, 1, 1);
  361.   } while( (*buf!=0xa) && (*buf!=13) );
  362.  
  363.  FRead(handle, buf, 1, 1);
  364.  
  365.  *cnt = (LONG) 0;
  366. }
  367.